home *** CD-ROM | disk | FTP | other *** search
- /*
- File: OrdColl.h
-
- Contains: Definition of class OrderedCollection
-
- Owned by: Reggie Adkins
-
- Copyright: © 1993 - 1996 by Apple Computer, Inc., all rights reserved.
-
- Change History (most recent first):
-
- <3> 5/24/96 jpa Fixed header.
- <2> 5/24/96 jpa 1339104: Made methods nonvirtual/inline for
- efficiency (EditorSetup)
-
- To Do:
- */
-
- #ifndef _ORDCOLL_
- #define _ORDCOLL_
-
- #ifndef _ODTYPES_
- #include "ODTypes.h"
- #endif
-
- #ifndef _PLFMDEF_
- #include "PlfmDef.h"
- #endif
-
- #ifndef _LINKLIST_
- #include "LinkList.h"
- #endif
-
- #ifndef _ODMEMORY_
- #include "ODMemory.h"
- #endif
-
- //==============================================================================
- // Theory of Operation
- //==============================================================================
-
- // OrdereCollection is an ordered collection of elements of type void* (since
- // we can't use templates)
- // Duplicates are allowed.
-
- //==============================================================================
- // Constants
- //==============================================================================
-
- //==============================================================================
- // Scalar Types
- //==============================================================================
-
- typedef void* ElementType;
-
- //=====================================================================================
- // Classes defined in this interface
- //=====================================================================================
-
- class OrderedCollection; // An ordered (not sorted) collection of ElementTypes
- class OrderedCollectionIterator;
-
- //=====================================================================================
- // Classes used by this interface
- //=====================================================================================
-
- class ValueLink; // A link plus a value of type ElementType.
-
- //=====================================================================================
- // Global Variables
- //=====================================================================================
-
- //=====================================================================================
- // Class ValueLink - Definition
- //=====================================================================================
-
- class ValueLink : public Link {
-
- public:
- ValueLink(ElementType value) { fValue = value;}
- virtual ~ValueLink() { }
- ElementType GetValue() { return fValue;}
- void SetValue(ElementType v) { fValue = v;}
-
- private:
- ElementType fValue;
- };
-
- //=====================================================================================
- // Class OrderedCollection
- //=====================================================================================
-
- class OrderedCollection
- {
-
- public:
-
- OrderedCollection();
- OrderedCollection(ODMemoryHeapID where);
- virtual ~OrderedCollection();
-
- ODULong Count() const { return fImplementation.Count(); };
-
- void AddFirst(ElementType element);
- void AddLast(ElementType element);
- void AddBefore(ElementType existing, ElementType tobeadded);
- void AddAfter(ElementType existing, ElementType tobeadded);
-
- ElementType After(ElementType existing) const;
- ElementType Before(ElementType existing) const;
-
- ElementType First() const;
- // Returns kODNULL if there is no first element.
- ElementType Last() const;
-
- ElementType RemoveFirst();
- // Don't call if there are no elements. Crash will result.
- ElementType RemoveLast();
- void RemoveAll() {fImplementation.DeleteAllLinks();}
-
- // Called from the destructor. Removes all elements, deleting the links
- // Does not delete the elements themselves
-
- void DeleteAll();
-
- // Removes and deletes all elements
-
- ODBoolean Remove(ElementType existing);
- // Returns true if existing was actually removed.
-
- ODBoolean Contains(ElementType existing) const;
-
- OrderedCollectionIterator* CreateIterator();
-
- inline ODMemoryHeapID GetHeap() const;
-
- protected:
- ValueLink* CreateNewLink(ElementType value) const;
- virtual ODBoolean ElementsMatch(ElementType v1,ElementType v2) const;
- // Does a pointer comparison by default. Subclasses may override.
-
- private:
- LinkedList fImplementation;
- ODMemoryHeapID fHeap; // if kODNULL, use default heap.
-
- friend class OrderedCollectionIterator;
- friend class ListIterator;
- };
-
- inline ODMemoryHeapID OrderedCollection::GetHeap() const
- {
- return fHeap;
- }
-
-
- //=====================================================================================
- // Class OrderedCollectionIterator
- //=====================================================================================
-
- class OrderedCollectionIterator {
- public:
- OrderedCollectionIterator(OrderedCollection* collection);
- ~OrderedCollectionIterator() { }
- inline ElementType First();
- inline ElementType Next();
- inline ElementType Last();
- inline ElementType Previous();
- ODBoolean IsNotComplete() {return fImplementation.IsNotComplete();}
- void RemoveCurrent() {fImplementation.RemoveCurrent();}
-
- private:
- OrderedCollection* fCollection;
- LinkedListIterator fImplementation;
- };
-
-
-
- //------------------------------------------------------------------------------
- // OrderedCollectionIterator::First
- //------------------------------------------------------------------------------
-
- ElementType OrderedCollectionIterator::First()
- {
- ValueLink* link = (ValueLink*) fImplementation.First();
-
- return link ? link->GetValue() : (ElementType)kODNULL;
- }
-
- //------------------------------------------------------------------------------
- // OrderedCollectionIterator::Next
- //------------------------------------------------------------------------------
-
- ElementType OrderedCollectionIterator::Next()
- {
- ValueLink* link = (ValueLink*) fImplementation.Next();
-
- return link ? link->GetValue() : (ElementType)kODNULL;
- }
-
- //------------------------------------------------------------------------------
- // OrderedCollectionIterator::Last
- //------------------------------------------------------------------------------
-
- ElementType OrderedCollectionIterator::Last()
- {
- ValueLink* link = (ValueLink*) fImplementation.Last();
-
- return link ? link->GetValue() : (ElementType)kODNULL;
- }
-
- //------------------------------------------------------------------------------
- // OrderedCollectionIterator::Previous
- //------------------------------------------------------------------------------
-
- ElementType OrderedCollectionIterator::Previous()
- {
- ValueLink* link = (ValueLink*) fImplementation.Previous();
-
- return link ? link->GetValue() : (ElementType)kODNULL;
- }
-
- #endif // _ORDCOLL_